import { AgentType } from "../../lib/util/friendData";
import { GCtrl } from "../../lib/util/GCtrl";


const { ccclass, property } = cc._decorator;

@ccclass
export default class HallMenuItem extends cc.Component {
    @property(cc.SpriteAtlas)
    atlas: cc.SpriteAtlas = null;

    @property(cc.Node)
    bg: cc.Node = null;
    @property(cc.Node)
    numNode: cc.Node = null;

    @property(cc.Label)
    numLb: cc.Label = null;

    @property(cc.SpriteFrame)
    sprFrm: cc.SpriteFrame[] = [];


    eventChooseMenuItem = null;
    type = 0;

    private nameLb: cc.Label = null;

    private myInfo: any = null;

    onLoad() {
        this.myInfo = cc.vv.session.getPlayer();
        this.nameLb = this.node.getChildByName('label').getComponent(cc.Label);
    }
    onEnable() {
        GCtrl.eventIndex['updateNewReqNum_hmi'] = cc.vv.eventCustom.on('updateNewReqNum', this.updateNewReqNum.bind(this), this.node);
    }
    onDisable() {
        GCtrl.eventIndex['updateNewReqNum_hmi'] && (cc.vv.eventCustom.off('updateNewReqNum', GCtrl.eventIndex['updateNewReqNum_hmi']), delete GCtrl.eventIndex['updateNewReqNum_hmi']);
    }

    onDestroy() { }
    start() { }

    init(type) {
        this.type = type;
        switch (this.type) {
            case cc.vv.GameChooseConfig.menu.chat: {
                this.nameLb.string = '聊天';
            } break;
            case cc.vv.GameChooseConfig.menu.addressBook: {
                this.nameLb.string = '通讯录';
            } break;
            case cc.vv.GameChooseConfig.menu.club: {
                this.nameLb.string = '俱乐部';
            } break;
            case cc.vv.GameChooseConfig.menu.personal: {
                this.nameLb.string = '个人中心';
            } break;
        }

        if (this.type == cc.vv.GameChooseConfig.menu.club &&
            this.myInfo.agentType == AgentType.user) {
            this.node.active = false;
        }
        else {
            this.node.active = true;
        }
        this.updateNewReqNum();
    }

    fill(type) {

        var _nameList = [
            ["GGZY_lt1", "GGZY_lt2"],//聊天
            ["GGZY_txl1", "GGZY_txl2"],//通讯录
            ["GGZY_jlb1", "GGZY_jlb2"],//俱乐部
            ["GGZY_grzx1", "GGZY_grzx2"],//个人中心
        ]
        //sprite
        var _sprite = this.bg.getComponent("cc.Sprite");
        if (_sprite) {
            var _name = "";
            let b = true;
            // if (this.type === type) {
            //     _name = _nameList[this.type][0];

            // } else {
            //     _name = _nameList[this.type][1];
            //     b = false;
            // }
            // _sprite.spriteFrame = this.atlas.getSpriteFrame(_name);
            // if (this.type == cc.vv.GameChooseConfig.menu.club) {
            this.type === type ? _sprite.spriteFrame = this.sprFrm[this.type * 2] :
                (_sprite.spriteFrame = this.sprFrm[this.type * 2 + 1], b = false);
            // }
            if (b)
                this.nameLb.node.color = new cc.Color().fromHEX('#02A9F1');
            else
                this.nameLb.node.color = new cc.Color().fromHEX('#323232');
        }
        this.node.width = this.bg.width;
    }
    private updateNewReqNum() {
        switch (this.type) {
            case cc.vv.GameChooseConfig.menu.chat: {
                let num = cc.vv.chatMgr.getAllNewMsgNum();
                if (num) {
                    this.numNode.active = true;
                    this.numLb.string = num > 99 ? '99+' : num + '';
                }
                else {
                    this.numNode.active = false;
                }
            } break;
            case cc.vv.GameChooseConfig.menu.addressBook: {
                let new_f_num = cc.vv.NewFriendData.getNewFriendNum();
                let new_g_num = cc.vv.GroupVerifyData.getNewGroupNum();
                let num = new_f_num + new_g_num;
                if (num) this.numNode.active = true;
                else this.numNode.active = false;
                this.numLb.string = num > 99 ? '99+' : num + '';
            } break;
            case cc.vv.GameChooseConfig.menu.club: { } break;
            case cc.vv.GameChooseConfig.menu.personal: { } break;
        }
        this.numLb.node.width = 36 + (this.numLb.string.length - 1) * 12;
    }


    onClickItemButton() {
        var _type = cc.vv.GameChooseConfig.getMenu();
        if (this.type == _type) {
            return;
        }
        cc.vv.GameChooseConfig.setMenu(this.type);
        cc.vv.audioUtil.playSound("Hall/sound/button_onclicked");
        cc.vv.eventCustom.emit("chooseMenuItem");
        cc.vv.eventCustom.emit('loadAgentData');
    }
}
